<p>This rule flags when <code>String#replace()</code> is used with a global regex pattern, or when <code>String#replaceAll()</code> is used with a
global regex flag.</p>
<h2>Why is this an issue?</h2>
<p>The <code>String#replaceAll()</code> method was introduced in ES2021 to provide a clearer and safer way to replace all occurrences of a pattern in
a string.</p>
<p>When using <code>String#replace()</code> with a global regex, developers must remember to include the global flag (<code>g</code>) and properly
escape special regex characters if the search pattern contains them. This can lead to bugs when special characters are not escaped correctly.</p>
<p>For example, if you want to replace all dots in a string, using <code>string.replace(/./g, '')</code> will actually replace all characters (since
<code>.</code> matches any character in regex), not just literal dots. You would need <code>string.replace(/\./g, '')</code> instead.</p>
<p>With <code>String#replaceAll()</code>, you can simply use <code>string.replaceAll('.', '')</code> which is both safer and more readable. The method
name clearly indicates that all occurrences will be replaced.</p>
<p>When <code>String#replaceAll()</code> is used with a regex, the global flag is required, or a <code>TypeError</code> will be thrown.</p>
<h3>What is the potential impact?</h3>
<p>Using the wrong replacement method can lead to unexpected behavior:</p>
<ul>
  <li> Incorrect replacements when special regex characters are not properly escaped </li>
  <li> Performance issues due to unnecessary regex compilation </li>
  <li> Reduced code readability and maintainability </li>
  <li> Potential security vulnerabilities if user input is used in regex patterns without proper escaping </li>
</ul>
<h3>How to fix?</h3>
<p>Replace <code>String#replace()</code> with global regex with <code>String#replaceAll()</code> using a string literal when the pattern doesn’t need
regex features.</p>
<h4>Non-compliant code example</h4>
<pre data-diff-id="1" data-diff-type="noncompliant">
const result = text.replace(/hello/g, 'hi'); // Noncompliant
</pre>
<h4>Compliant code example</h4>
<pre data-diff-id="1" data-diff-type="compliant">
const result = text.replaceAll('hello', 'hi');
</pre>
<h3>Documentation</h3>
<ul>
  <li> <a href="https://github.com/sindresorhus/eslint-plugin-unicorn#readme">eslint-plugin-unicorn</a> - Rule <a
  href="https://github.com/sindresorhus/eslint-plugin-unicorn/blob/HEAD/docs/rules/prefer-string-replace-all.md">prefer-string-replace-all</a> </li>
  <li> MDN: String.prototype.replaceAll() - <a
  href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll">Official documentation for the
  String.replaceAll() method</a> </li>
  <li> MDN: String.prototype.replace() - <a
  href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace">Official documentation for the
  String.replace() method</a> </li>
</ul>
